<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    

<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="utf-8">
    <meta name="keywords" content="WebRTC06入门教程03, 炫杉">
    <meta name="description" content="信令服务器分析

Signaling信令和协商Signaling and Negotiation用户建立连接的过程以信令和协商为主。有下面几个步骤：

为peer连接创建一个潜在候选项的list
用户A或应用选择一个要连接的用户B
信令层通">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>WebRTC06入门教程03 | 炫杉</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/css/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-2.2.0.min.js"></script>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/css/prism-line-numbers.css" type="text/css"></head>

</head>

<body>

<header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="container">
            <div class="nav-wrapper">
                <div class="brand-logo">
                    <a href="/" class="waves-effect waves-light">
                        
                        <img src="/medias/logo.png" class="logo-img hide-on-small-only">
                        
                        <span class="logo-span">炫杉</span>
                    </a>
                </div>
                <a href="#" data-activates="mobile-nav" class="button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right">
    
    <li class="hide-on-med-and-down">
        <a href="/" class="waves-effect waves-light">
            
            <i class="fa fa-home"></i>
            
            <span>Index</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/tags" class="waves-effect waves-light">
            
            <i class="fa fa-tags"></i>
            
            <span>Tags</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/categories" class="waves-effect waves-light">
            
            <i class="fa fa-bookmark"></i>
            
            <span>Categories</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/archives" class="waves-effect waves-light">
            
            <i class="fa fa-archive"></i>
            
            <span>Archives</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/about" class="waves-effect waves-light">
            
            <i class="fa fa-user-circle-o"></i>
            
            <span>About</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="https://oo1.win" class="waves-effect waves-light">
            
            <i class="fa fa-star"></i>
            
            <span>Bookmark</span>
        </a>
    </li>
    
    <li>
        <a id="toggleSearch" class="waves-effect waves-light">
            <i id="searchIcon" class="mdi-action-search"></i>
        </a>
    </li>

</ul>

<div class="side-nav" id="mobile-nav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">炫杉</div>
        <div class="logo-desc">
            
            提着鸟笼的老头站在一旁拍我肩膀 我猜他想的应该和我一样
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li>
            <a href="/" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-home"></i>
                
                Index
            </a>
        </li>
        
        <li>
            <a href="/tags" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-tags"></i>
                
                Tags
            </a>
        </li>
        
        <li>
            <a href="/categories" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-bookmark"></i>
                
                Categories
            </a>
        </li>
        
        <li>
            <a href="/archives" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-archive"></i>
                
                Archives
            </a>
        </li>
        
        <li>
            <a href="/about" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-user-circle-o"></i>
                
                About
            </a>
        </li>
        
        <li>
            <a href="https://oo1.win" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-star"></i>
                
                Bookmark
            </a>
        </li>
        
        
    </ul>

    <div class="social-link"><a href="https://github.com/ixsim" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
    <i class="fa fa-github"></i>
</a>

<a href="mailto:chenjiayin1990@163.com" class="tooltipped" target="_blank" data-tooltip="邮件:chinalixs@qq.com" data-position="top" data-delay="50">
    <i class="fa fa-envelope-open"></i>
</a>

<a href="http://wpa.qq.com/msgrd?v=3&uin=6956820&site=qq&menu=yes" class="tooltipped" data-tooltip="QQ: 6956820" data-position="top" data-delay="50">
    <i class="fa fa-qq"></i>
</a>
</div>
</div>

            </div>
        </div>

        
    </nav>
</header>



<div class="bg-cover post-cover" style="background-image: url('/medias/featureimages/3.jpg')">
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        WebRTC06入门教程03
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1,
    #articleContent h2,
    #articleContent h3,
    #articleContent h4,
    #articleContent h5,
    #articleContent h6 {
        padding-top: 76px;
        margin-top: -76px;
    }

    #articleContent h1 {
        line-height: 3.5rem;
    }

    #articleContent h2 {
        line-height: 3.2rem;
    }

    #articleContent h3 {
        line-height: 2.8rem;
    }

    #articleContent h4 {
        line-height: 2.5rem;
    }

    #articleContent h5 {
        line-height: 2.2rem;
    }

    #articleContent h6 {
        line-height: 1.9rem;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }
</style>
<div class="row">
    <div class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            
            <div class="article-tag">
                
                <a href="/tags/Demo/" target="_blank">
                    <span class="chip bg-color">Demo</span>
                </a>
                
                <a href="/tags/tutorial/" target="_blank">
                    <span class="chip bg-color">tutorial</span>
                </a>
                
            </div>
            
            <div class="post-info">
                
                <span class="post-cate">
                    <i class="fa fa-bookmark fa-fw icon-category"></i>
                    
                    <a href="/categories/WebRTC/" class="post-category" target="_blank">
                        WebRTC
                    </a>
                    
                </span>
                

                <span class="post-date">
                    <i class="fa fa-clock-o fa-fw"></i>2018-08-31
                </span>
            </div>
        </div>
        <hr>
        <div class="card-content article-card-content">
            <div id="articleContent">
                <p>信令服务器分析</p>
<a id="more"></a>
<h1 id="Signaling"><a href="#Signaling" class="headerlink" title="Signaling"></a>Signaling</h1><h2 id="信令和协商Signaling-and-Negotiation"><a href="#信令和协商Signaling-and-Negotiation" class="headerlink" title="信令和协商Signaling and Negotiation"></a>信令和协商Signaling and Negotiation</h2><p>用户建立连接的过程以信令和协商为主。有下面几个步骤：</p>
<ul>
<li>为peer连接创建一个潜在候选项的list</li>
<li>用户A或应用选择一个要连接的用户B</li>
<li>信令层通知用户B有人想和你连。他可以同意/拒绝</li>
<li>用户A被通知offer同意</li>
<li>用户A初始化与B的RTCPeerConnection</li>
<li>俩用户通过信令服务器交换软硬件信息</li>
<li>俩用户交换本地信息</li>
<li>连接成功/失败</li>
</ul>
<h2 id="建立服务器"><a href="#建立服务器" class="headerlink" title="建立服务器"></a>建立服务器</h2><p>我们现在来建立一个自己的信令机制。允许一个用户呼叫另一个。一旦一个用户被呼叫，服务器在他们之间传递offer,answer,ICE候选。然后建立连接。</p>
<p><img src="https://www.tutorialspoint.com/webrtc/images/building_the_server.jpg" alt="信令服务器"></p>
<p>上个图是用户使用信令服务器时的信息流。</p>
<p>首先，每个用户在服务器注册。传一个简单的字符串username即可。注册后的用户可以相互呼叫。用户1发一个带有他想呼叫的用户id的offer。其他人应当answer。最后，在用户中间发送ICE候选直到他们建立连接。</p>
<p>去创建一个WebRTC连接服务器必须有能力在没有使用WebRTCpeer连接的情况下发送messages。因此我们使用HTML5 WebSockets—一个两端(一个web服务器一个web浏览器)的双向socket连接。现在我们开始使用WebSocket库。创建<code>server.js</code>文件并加入以下代码：</p>
<pre class="line-numbers language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">//依赖ws库 声明一个WebSocketServer服务器</span>
<span class="token keyword">var</span> WebSocketServer <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'ws'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Server<span class="token punctuation">;</span> 

<span class="token comment" spellcheck="true">//创建端口9090的WebSocket服务器</span>
<span class="token keyword">var</span> wss <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">WebSocketServer</span><span class="token punctuation">(</span><span class="token punctuation">{</span>port<span class="token punctuation">:</span> <span class="token number">9090</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 

<span class="token comment" spellcheck="true">//当有一个用户接入服务器</span>
wss<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'connection'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>connection<span class="token punctuation">)</span> <span class="token punctuation">{</span> 
   console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"user connected"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

   <span class="token comment" spellcheck="true">//服务器收到已接入用户的消息</span>
   connection<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'message'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">{</span> 
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Got message from a user:"</span><span class="token punctuation">,</span> message<span class="token punctuation">)</span><span class="token punctuation">;</span> 
   <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 

   connection<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">"Hello from server"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>先要求WebSocket库。再创建9090端口的socket服务器。下一步，监听connection事件。监听用户发来的任何消息。最后我们发送一个回复给已连接的用户说Hello from Server.</p>
<p>终端运行<code>node server</code>服务器就跑起来了。</p>
<p>为了测试服务器。我们用<code>wscat</code>这个程序。他可以直接连入服务器并测试命令。先在终端让服务器跑起来之后，再开一个终端，运行<code>wscat -c ws://localhost:9090</code>命令。</p>
<h2 id="用户注册"><a href="#用户注册" class="headerlink" title="用户注册"></a>用户注册</h2><p>在我们的信令服务器中，我们将使用基于字符串的username来让我们知道信息的去向。下面更改一下connection的句柄。</p>
<pre class="line-numbers language-javascript"><code class="language-javascript">connection<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'message'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span> <span class="token punctuation">{</span> 
   <span class="token keyword">var</span> data<span class="token punctuation">;</span> 

   <span class="token comment" spellcheck="true">//只接收JSON</span>
   <span class="token keyword">try</span> <span class="token punctuation">{</span> 
      data <span class="token operator">=</span> JSON<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span> 
   <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">e</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Invalid JSON"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
      data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span> 
   <span class="token punctuation">}</span> 

<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>这样我们就做到了只接收JSON。下一步我们需要存储所有连接到用户。使用一个简单的js对象。</p>
<p>在我们的文件头部增加一句<code>var users={};</code>。</p>
<p><strong>这个数组里存的都是connection,而下标就是name。</strong></p>
<p>现在我们还要为每一个从客户端发来的message增加一个<em>type</em>域。例如是登录就发<em>login</em> type的message。现在来定义。</p>
<pre class="line-numbers language-javascript"><code class="language-javascript">connection<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'message'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">{</span>
   <span class="token keyword">var</span> data<span class="token punctuation">;</span> 

   <span class="token comment" spellcheck="true">//只接收JSON</span>
   <span class="token keyword">try</span> <span class="token punctuation">{</span> 
      data <span class="token operator">=</span> JSON<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//是JSON就给它分组</span>
   <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">e</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Invalid JSON"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
      data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span> 
   <span class="token punctuation">}</span>

   <span class="token comment" spellcheck="true">//分开各种type </span>
   <span class="token keyword">switch</span> <span class="token punctuation">(</span>data<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span> 
      <span class="token comment" spellcheck="true">//一个用户尝试登录</span>
      <span class="token keyword">case</span> <span class="token string">"login"</span><span class="token punctuation">:</span> 
         console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"User logged:"</span><span class="token punctuation">,</span> data<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 

         <span class="token comment" spellcheck="true">//存在这个name则拒绝登录</span>
         <span class="token keyword">if</span><span class="token punctuation">(</span>users<span class="token punctuation">[</span>data<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
            <span class="token function">sendTo</span><span class="token punctuation">(</span>connection<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
               type<span class="token punctuation">:</span> <span class="token string">"login"</span><span class="token punctuation">,</span> 
               success<span class="token punctuation">:</span> <span class="token boolean">false</span> 
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
         <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> 
            <span class="token comment" spellcheck="true">//保存这个用户连接</span>
            users<span class="token punctuation">[</span>data<span class="token punctuation">.</span>name<span class="token punctuation">]</span> <span class="token operator">=</span> connection<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//connection进入users[]</span>
            connection<span class="token punctuation">.</span>name <span class="token operator">=</span> data<span class="token punctuation">.</span>name<span class="token punctuation">;</span> 

            <span class="token function">sendTo</span><span class="token punctuation">(</span>connection<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
               type<span class="token punctuation">:</span> <span class="token string">"login"</span><span class="token punctuation">,</span> 
               success<span class="token punctuation">:</span> <span class="token boolean">true</span> 
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

         <span class="token punctuation">}</span> 

         <span class="token keyword">break</span><span class="token punctuation">;</span>

      <span class="token keyword">default</span><span class="token punctuation">:</span> 
         <span class="token function">sendTo</span><span class="token punctuation">(</span>connection<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
            type<span class="token punctuation">:</span> <span class="token string">"error"</span><span class="token punctuation">,</span> 
            message<span class="token punctuation">:</span> <span class="token string">"Command no found: "</span> <span class="token operator">+</span> data<span class="token punctuation">.</span>type 
         <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 

         <span class="token keyword">break</span><span class="token punctuation">;</span> 
   <span class="token punctuation">}</span> 

<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>下面这一段代码是发送消息到连接的辅助函数。这个函数可以确保我们发送的消息都是JSON格式。</p>
<pre class="line-numbers language-javascript"><code class="language-javascript"><span class="token keyword">function</span> <span class="token function">sendTo</span><span class="token punctuation">(</span>connection<span class="token punctuation">,</span> message<span class="token punctuation">)</span> <span class="token punctuation">{</span> 
   connection<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>JSON<span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p>你可以在wscat发送一段json来测试一下了。</p>
<h2 id="呼叫"><a href="#呼叫" class="headerlink" title="呼叫"></a>呼叫</h2><p>登录后的用户想要呼叫就要做一个offer发给别人。现在我们来添加<em>offer</em>句柄。</p>
<pre class="line-numbers language-javascript"><code class="language-javascript"><span class="token keyword">case</span> <span class="token string">"offer"</span><span class="token punctuation">:</span> 
   <span class="token comment" spellcheck="true">//for ex. A</span>
   console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Sending offer to: "</span><span class="token punctuation">,</span> data<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 

   <span class="token comment" spellcheck="true">//如果B存在则发送A的offer细节</span>
   <span class="token keyword">var</span> conn <span class="token operator">=</span> users<span class="token punctuation">[</span>data<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span> 

   <span class="token keyword">if</span><span class="token punctuation">(</span>conn <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span> 
      <span class="token comment" spellcheck="true">//设置A的连接对象是B</span>
      connection<span class="token punctuation">.</span>otherName <span class="token operator">=</span> data<span class="token punctuation">.</span>name<span class="token punctuation">;</span>
       <span class="token comment" spellcheck="true">//A的offer发送给B</span>
      <span class="token function">sendTo</span><span class="token punctuation">(</span>conn<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
         type<span class="token punctuation">:</span> <span class="token string">"offer"</span><span class="token punctuation">,</span> 
         offer<span class="token punctuation">:</span> data<span class="token punctuation">.</span>offer<span class="token punctuation">,</span> 
         name<span class="token punctuation">:</span> connection<span class="token punctuation">.</span>name 
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
   <span class="token punctuation">}</span>

   <span class="token keyword">break</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="接听"><a href="#接听" class="headerlink" title="接听"></a>接听</h2><p>接听回话和<em>offer</em>句柄类似。在<em>offer</em>句柄后面加入<em>answer</em>，answer的内容无论是什么都会被发送。</p>
<pre class="line-numbers language-javascript"><code class="language-javascript"><span class="token keyword">case</span> <span class="token string">"answer"</span><span class="token punctuation">:</span> 
   console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Sending answer to: "</span><span class="token punctuation">,</span> data<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 

   <span class="token comment" spellcheck="true">//for ex. UserB answers UserA </span>
   <span class="token keyword">var</span> conn <span class="token operator">=</span> users<span class="token punctuation">[</span>data<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span> 

   <span class="token keyword">if</span><span class="token punctuation">(</span>conn <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
       <span class="token comment" spellcheck="true">//同理回了人家的answer你的连接对象也就是他了</span>
      connection<span class="token punctuation">.</span>otherName <span class="token operator">=</span> data<span class="token punctuation">.</span>name<span class="token punctuation">;</span> 
      <span class="token function">sendTo</span><span class="token punctuation">(</span>conn<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
         type<span class="token punctuation">:</span> <span class="token string">"answer"</span><span class="token punctuation">,</span> 
         answer<span class="token punctuation">:</span> data<span class="token punctuation">.</span>answer 
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
   <span class="token punctuation">}</span>

   <span class="token keyword">break</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>在例子中，offer和answer都是简单的字符串。但在真正的应用中他们会被SDP数据填充。</p>
<h2 id="ICE候选"><a href="#ICE候选" class="headerlink" title="ICE候选"></a>ICE候选</h2><p>最后一部分是处理用户之间的ICE候选。我们在用户中间用相同的机制通过message。主要的不同就是候选消息可能会多次、无序地发送给每个用户。添加<em>candidate</em>句柄：</p>
<pre class="line-numbers language-javascript"><code class="language-javascript"><span class="token keyword">case</span> <span class="token string">"candidate"</span><span class="token punctuation">:</span> 
   console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Sending candidate to:"</span><span class="token punctuation">,</span>data<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
   <span class="token keyword">var</span> conn <span class="token operator">=</span> users<span class="token punctuation">[</span>data<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span> 

   <span class="token keyword">if</span><span class="token punctuation">(</span>conn <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">sendTo</span><span class="token punctuation">(</span>conn<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
         type<span class="token punctuation">:</span> <span class="token string">"candidate"</span><span class="token punctuation">,</span> 
         candidate<span class="token punctuation">:</span> data<span class="token punctuation">.</span>candidate 
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
   <span class="token punctuation">}</span>

   <span class="token keyword">break</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="断开对话"><a href="#断开对话" class="headerlink" title="断开对话"></a>断开对话</h2><p>为了让用户之间断开对话。我们要一个挂起函数。她同时会告诉服务器删掉所有的用户引用。添加<em>leave</em>句柄：</p>
<pre class="line-numbers language-javascript"><code class="language-javascript"><span class="token keyword">case</span> <span class="token string">"leave"</span><span class="token punctuation">:</span> 
   console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Disconnecting from"</span><span class="token punctuation">,</span> data<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
   <span class="token keyword">var</span> conn <span class="token operator">=</span> users<span class="token punctuation">[</span>data<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span> 
   conn<span class="token punctuation">.</span>otherName <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> 

   <span class="token comment" spellcheck="true">//notify the other user so he can disconnect his peer connection </span>
   <span class="token keyword">if</span><span class="token punctuation">(</span>conn <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
      <span class="token function">sendTo</span><span class="token punctuation">(</span>conn<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
         type<span class="token punctuation">:</span> <span class="token string">"leave"</span> 
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
   <span class="token punctuation">}</span> 

   <span class="token keyword">break</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>同时吧leave事件发送给另一个用户，让他也你能相应的断开对话。</p>
<p>当用户从信令服务器中丢弃掉连接的时候我们还需要有<em>close</em>句柄。</p>
<pre class="line-numbers language-javascript"><code class="language-javascript">connection<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">"close"</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 

   <span class="token keyword">if</span><span class="token punctuation">(</span>connection<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token punctuation">{</span> 
      <span class="token keyword">delete</span> users<span class="token punctuation">[</span>connection<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span> 

      <span class="token keyword">if</span><span class="token punctuation">(</span>connection<span class="token punctuation">.</span>otherName<span class="token punctuation">)</span> <span class="token punctuation">{</span> 
         console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Disconnecting from "</span><span class="token punctuation">,</span> connection<span class="token punctuation">.</span>otherName<span class="token punctuation">)</span><span class="token punctuation">;</span> 
         <span class="token keyword">var</span> conn <span class="token operator">=</span> users<span class="token punctuation">[</span>connection<span class="token punctuation">.</span>otherName<span class="token punctuation">]</span><span class="token punctuation">;</span> 
         conn<span class="token punctuation">.</span>otherName <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>

         <span class="token keyword">if</span><span class="token punctuation">(</span>conn <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
            <span class="token function">sendTo</span><span class="token punctuation">(</span>conn<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
               type<span class="token punctuation">:</span> <span class="token string">"leave"</span> 
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
         <span class="token punctuation">}</span>  
      <span class="token punctuation">}</span> 
   <span class="token punctuation">}</span> 
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>信令服务器完成。</p>
<h2 id="完整的代码"><a href="#完整的代码" class="headerlink" title="完整的代码"></a>完整的代码</h2><pre class="line-numbers language-javascript"><code class="language-javascript"><span class="token comment" spellcheck="true">//依赖ws库</span>
<span class="token keyword">var</span> WebSocketServer <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'ws'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Server<span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">//开9090端口。服务器名字叫wss</span>
<span class="token keyword">var</span> wss <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">WebSocketServer</span><span class="token punctuation">(</span><span class="token punctuation">{</span>port<span class="token punctuation">:</span> <span class="token number">9090</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 

<span class="token comment" spellcheck="true">//用户群组。里面存的是connection。下标是name。</span>
<span class="token keyword">var</span> users <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">//有人要连接时 </span>
wss<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'connection'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>connection<span class="token punctuation">)</span> <span class="token punctuation">{</span>

   console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"User connected"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

   <span class="token comment" spellcheck="true">//服务器收到了message</span>
   connection<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'message'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span> <span class="token punctuation">{</span> 

      <span class="token keyword">var</span> data<span class="token punctuation">;</span> 
      <span class="token comment" spellcheck="true">//只接收JSON </span>
      <span class="token keyword">try</span> <span class="token punctuation">{</span>
         data <span class="token operator">=</span> JSON<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//切分JSON存入data</span>
      <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">e</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
         console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Invalid JSON"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
         data <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span> 
      <span class="token punctuation">}</span> 

      <span class="token comment" spellcheck="true">//用户消息的type </span>
      <span class="token keyword">switch</span> <span class="token punctuation">(</span>data<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span> 
         <span class="token comment" spellcheck="true">//尝试登录</span>

         <span class="token keyword">case</span> <span class="token string">"login"</span><span class="token punctuation">:</span> 
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"User logged"</span><span class="token punctuation">,</span> data<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 

            <span class="token comment" spellcheck="true">//重名了 </span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>users<span class="token punctuation">[</span>data<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
               <span class="token function">sendTo</span><span class="token punctuation">(</span>connection<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
                  type<span class="token punctuation">:</span> <span class="token string">"login"</span><span class="token punctuation">,</span> 
                  success<span class="token punctuation">:</span> <span class="token boolean">false</span> 
               <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> 
               <span class="token comment" spellcheck="true">//可以登则存入数组 </span>
               users<span class="token punctuation">[</span>data<span class="token punctuation">.</span>name<span class="token punctuation">]</span> <span class="token operator">=</span> connection<span class="token punctuation">;</span> 
               connection<span class="token punctuation">.</span>name <span class="token operator">=</span> data<span class="token punctuation">.</span>name<span class="token punctuation">;</span> 

               <span class="token function">sendTo</span><span class="token punctuation">(</span>connection<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
                  type<span class="token punctuation">:</span> <span class="token string">"login"</span><span class="token punctuation">,</span> 
                  success<span class="token punctuation">:</span> <span class="token boolean">true</span> 
               <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
            <span class="token punctuation">}</span> 

            <span class="token keyword">break</span><span class="token punctuation">;</span> 

         <span class="token keyword">case</span> <span class="token string">"offer"</span><span class="token punctuation">:</span> 
            <span class="token comment" spellcheck="true">//for ex. A想打给B </span>
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Sending offer to: "</span><span class="token punctuation">,</span> data<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 

            <span class="token comment" spellcheck="true">//B存在则发给B offer </span>
            <span class="token keyword">var</span> conn <span class="token operator">=</span> users<span class="token punctuation">[</span>data<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span>

            <span class="token keyword">if</span><span class="token punctuation">(</span>conn <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
               <span class="token operator">/</span>A 的连接对象设为 B
               connection<span class="token punctuation">.</span>otherName <span class="token operator">=</span> data<span class="token punctuation">.</span>name<span class="token punctuation">;</span> 

               <span class="token function">sendTo</span><span class="token punctuation">(</span>conn<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
                  type<span class="token punctuation">:</span> <span class="token string">"offer"</span><span class="token punctuation">,</span> 
                  offer<span class="token punctuation">:</span> data<span class="token punctuation">.</span>offer<span class="token punctuation">,</span> 
                  name<span class="token punctuation">:</span> connection<span class="token punctuation">.</span>name 
               <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
            <span class="token punctuation">}</span> 

            <span class="token keyword">break</span><span class="token punctuation">;</span>  

         <span class="token keyword">case</span> <span class="token string">"answer"</span><span class="token punctuation">:</span> 
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Sending answer to: "</span><span class="token punctuation">,</span> data<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
            <span class="token comment" spellcheck="true">//for ex. B要接听A</span>
            <span class="token keyword">var</span> conn <span class="token operator">=</span> users<span class="token punctuation">[</span>data<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span> 

            <span class="token keyword">if</span><span class="token punctuation">(</span>conn <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
               connection<span class="token punctuation">.</span>otherName <span class="token operator">=</span> data<span class="token punctuation">.</span>name<span class="token punctuation">;</span> 
               <span class="token function">sendTo</span><span class="token punctuation">(</span>conn<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
                  type<span class="token punctuation">:</span> <span class="token string">"answer"</span><span class="token punctuation">,</span> 
                  answer<span class="token punctuation">:</span> data<span class="token punctuation">.</span>answer 
               <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
            <span class="token punctuation">}</span> 

            <span class="token keyword">break</span><span class="token punctuation">;</span>  

         <span class="token keyword">case</span> <span class="token string">"candidate"</span><span class="token punctuation">:</span> 
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Sending candidate to:"</span><span class="token punctuation">,</span>data<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
            <span class="token keyword">var</span> conn <span class="token operator">=</span> users<span class="token punctuation">[</span>data<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span>  

            <span class="token keyword">if</span><span class="token punctuation">(</span>conn <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
               <span class="token function">sendTo</span><span class="token punctuation">(</span>conn<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
                  type<span class="token punctuation">:</span> <span class="token string">"candidate"</span><span class="token punctuation">,</span> 
                  candidate<span class="token punctuation">:</span> data<span class="token punctuation">.</span>candidate 
               <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> 

            <span class="token keyword">break</span><span class="token punctuation">;</span>  

         <span class="token keyword">case</span> <span class="token string">"leave"</span><span class="token punctuation">:</span> 
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Disconnecting from"</span><span class="token punctuation">,</span> data<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> 
            <span class="token keyword">var</span> conn <span class="token operator">=</span> users<span class="token punctuation">[</span>data<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span> 
            conn<span class="token punctuation">.</span>otherName <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> 

            <span class="token comment" spellcheck="true">//notify the other user so he can disconnect his peer connection </span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>conn <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
               <span class="token function">sendTo</span><span class="token punctuation">(</span>conn<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
                  type<span class="token punctuation">:</span> <span class="token string">"leave"</span> 
               <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
            <span class="token punctuation">}</span>  

            <span class="token keyword">break</span><span class="token punctuation">;</span>  

         <span class="token keyword">default</span><span class="token punctuation">:</span> 
            <span class="token function">sendTo</span><span class="token punctuation">(</span>connection<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
               type<span class="token punctuation">:</span> <span class="token string">"error"</span><span class="token punctuation">,</span> 
               message<span class="token punctuation">:</span> <span class="token string">"Command not found: "</span> <span class="token operator">+</span> data<span class="token punctuation">.</span>type 
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 

            <span class="token keyword">break</span><span class="token punctuation">;</span> 
      <span class="token punctuation">}</span>  
   <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  

   <span class="token comment" spellcheck="true">//用户退出，比如关闭了浏览器窗口 </span>
   <span class="token comment" spellcheck="true">//如果我们仍在offer\answer\candidate状态。则起作用。 </span>
   connection<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">"close"</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 

      <span class="token keyword">if</span><span class="token punctuation">(</span>connection<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token punctuation">{</span> 
      <span class="token keyword">delete</span> users<span class="token punctuation">[</span>connection<span class="token punctuation">.</span>name<span class="token punctuation">]</span><span class="token punctuation">;</span> 

         <span class="token keyword">if</span><span class="token punctuation">(</span>connection<span class="token punctuation">.</span>otherName<span class="token punctuation">)</span> <span class="token punctuation">{</span> 
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Disconnecting from "</span><span class="token punctuation">,</span> connection<span class="token punctuation">.</span>otherName<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//A的连接对B象存到Conn</span>
            <span class="token keyword">var</span> conn <span class="token operator">=</span> users<span class="token punctuation">[</span>connection<span class="token punctuation">.</span>otherName<span class="token punctuation">]</span><span class="token punctuation">;</span> 
            <span class="token comment" spellcheck="true">//设置A为无连接状态</span>
            conn<span class="token punctuation">.</span>otherName <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>  
                <span class="token comment" spellcheck="true">//让B也leave会话</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>conn <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> 
               <span class="token function">sendTo</span><span class="token punctuation">(</span>conn<span class="token punctuation">,</span> <span class="token punctuation">{</span> 
                  type<span class="token punctuation">:</span> <span class="token string">"leave"</span> 
               <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>  
         <span class="token punctuation">}</span> 
      <span class="token punctuation">}</span> 
   <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  

   connection<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">"Hello world"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 

<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  

<span class="token keyword">function</span> <span class="token function">sendTo</span><span class="token punctuation">(</span>connection<span class="token punctuation">,</span> message<span class="token punctuation">)</span> <span class="token punctuation">{</span> 
   connection<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>JSON<span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>这篇文章一定要和入门教程02一起看~</p>

            </div>
            <hr>

            

            <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    <div class="social-share" data-disabled="qzone" data-wechat-qrcode-helper="<p>微信里点“发现”->“扫一扫”二维码便可查看分享。</p>"></div>
    
</div>

<script src="/libs/share/js/social-share.min.js"></script>

            <div class="reprint">
                <p>
                    <span class="reprint-tip">转载请注明: </span>
                    <a href="https://xuanshan.gitee.io" class="b-link-green">炫杉</a>
                    <i class="fa fa-angle-right fa-lg fa-fw text-color"></i>
                    <a href="/WebRTC/WebRTC06入门教程03/" class="b-link-green">WebRTC06入门教程03</a>
                </p>
            </div>
        </div>
    </div>

    

    

    

    
    <div class="livere-card card" data-aos="fade-up">
    <!-- 来必力City版安装代码 -->
    <div id="lv-container" class="card-content" data-id="city" data-uid="MTAyMC8yOTg2My82NDI5">
        <script type="text/javascript">
            (function (d, s) {
                let j, e = d.getElementsByTagName(s)[0];
                if (typeof LivereTower === 'function') {
                    return;
                }

                j = d.createElement(s);
                j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
                j.async = true;

                e.parentNode.insertBefore(j, e);
            })(document, 'script');
        </script>
        <noscript>为正常使用来必力评论功能请激活JavaScript。</noscript>
    </div>
    <!-- City版安装代码已完成 -->
</div>
    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">上一篇</div>
            <div class="card">
                <a href="/Algorithm/ListNode_easy/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/21.jpg" class="responsive-img" alt="LeetCode 链表Esay题">
                        
                        <span class="card-title">LeetCode 链表Esay题</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">知识点：ListNode单链表、 快慢指针

基础部分这段代码便于在IDE调试。
#include&lt;iostream>
using namespace std;
typedef struct ListNode
{
    int va</div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="fa fa-clock-o fa-fw icon-date"></i>2018-09-06
                        </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Algorithm/" class="post-category" target="_blank">
                                    Algorithm
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/C/" target="_blank">
                        <span class="chip bg-color">C++</span>
                    </a>
                    
                    <a href="/tags/LeetCode/" target="_blank">
                        <span class="chip bg-color">LeetCode</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">下一篇</div>
            <div class="card">
                <a href="/WebRTC/WebRTC05入门教程02/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/1.jpg" class="responsive-img" alt="WebRTC05入门教程02">
                        
                        <span class="card-title">WebRTC05入门教程02</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary">p2p的核心RTCPeerConnection APIs的介绍

RTCPeerConnection APIs简单样例：var pc = RTCPeerConnection(config);
config包含 key, iceServers</div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2018-08-30
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/WebRTC/" class="post-category" target="_blank">
                                    WebRTC
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Demo/" target="_blank">
                        <span class="chip bg-color">Demo</span>
                    </a>
                    
                    <a href="/tags/tutorial/" target="_blank">
                        <span class="chip bg-color">tutorial</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>
</div>
    </div>
    <div class="col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title">目录</div>
            <div id="toc-content">

            </div>
        </div>
    </div>
</div>

<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingSelector: 'h1,h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1,h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });
    });
</script>
    

</main>

<footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            &copy;<a href="https://o--o.win" target="_blank">炫杉2018</a> 
            <a href="https://hexo.io/" target="_blank">Hexo</a> 强力驱动
              主题 <a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">matery</a>        </div>
        <div class="col s12 m4 l4 social-link"><a href="https://github.com/ixsim" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
    <i class="fa fa-github"></i>
</a>

<a href="mailto:chenjiayin1990@163.com" class="tooltipped" target="_blank" data-tooltip="邮件:chinalixs@qq.com" data-position="top" data-delay="50">
    <i class="fa fa-envelope-open"></i>
</a>

<a href="http://wpa.qq.com/msgrd?v=3&uin=6956820&site=qq&menu=yes" class="tooltipped" data-tooltip="QQ: 6956820" data-position="top" data-delay="50">
    <i class="fa fa-qq"></i>
</a>
</div>
    </div>
</footer>

<div class="progress-bar"></div>


<!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title">搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字" class="search-input" autofocus>
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
</script>
<!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fa fa-angle-up"></i>
    </a>
</div>


<script src="/libs/materialize/js/materialize.min.js"></script>
<script src="/libs/masonry/masonry.pkgd.min.js"></script>
<script src="/libs/aos/aos.js"></script>
<script src="/libs/scrollprogress/scrollProgress.min.js"></script>
<script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
<script src="/js/matery.js"></script>
</body>
</html>